package so.sao.shop.supplier.web.authorized;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import so.sao.shop.supplier.config.Constant;
import so.sao.shop.supplier.domain.User;
import so.sao.shop.supplier.pojo.Result;
import so.sao.shop.supplier.pojo.SystemContext;
import so.sao.shop.supplier.pojo.input.auth.AdminUserStatusInput;
import so.sao.shop.supplier.pojo.input.auth.ChildrenAccountInput;
import so.sao.shop.supplier.pojo.vo.authorized.ChildrenAccountVo;
import so.sao.shop.supplier.service.authorized.ChildrenAccountService;
import so.sao.shop.supplier.service.authorized.PermissionService;
import so.sao.shop.supplier.util.JwtTokenUtil;

import javax.validation.Valid;

/**
 * <p>Version: supplier V1.1.0 </p>
 * <p>Title: AdminController</p>
 * <p>Description: 管理员子账号Controller</p>
 *
 * @author: taowang
 * @Date: Created in 2017/11/2 11:38
 */
@RestController
@RequestMapping("/subaccount")
public class ChildrenAccountController {

    @Autowired
    private ChildrenAccountService childrenAccountService;
    @Autowired
    private PermissionService permissionService;

    @RequestMapping(value="",method=RequestMethod.PUT)
    public Result<?> updateChildrenAccount(@Valid @RequestBody ChildrenAccountVo childrenAccountVo) {
    	return childrenAccountService.updateChildrenAccount(childrenAccountVo);
    }

    @PutMapping("/updateUserStatus")
    public Result<?> updateUserStatus(@Valid @RequestBody AdminUserStatusInput userStatusInput){
        User user = JwtTokenUtil.getCurrentUser();
        if(user == null){
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        return childrenAccountService.updateUserStatus(userStatusInput);
    }

    @GetMapping("/findMenuPermission")
    public Result<?> findMenuPermission(){
        User user = JwtTokenUtil.getCurrentUser();
        return permissionService.findMenuPermission(user.getId(),null);
    }

    @GetMapping("/findButtonPermission/{flag}")
    public Result<?> findButtonPermission(@PathVariable String flag){
        User user = JwtTokenUtil.getCurrentUser();
        return permissionService.findButtonPermission(user.getId(),flag);
    }

	@RequestMapping(value="/save",method=RequestMethod.POST)
	public Result<?> save(@Valid @RequestBody ChildrenAccountVo childrenAccountVo) {
		//获取用户
        User user = JwtTokenUtil.getCurrentUser();
        childrenAccountVo.setParentId(user.getId());
        childrenAccountVo.setIsAdmin(user.getIsAdmin());
        return childrenAccountService.save(childrenAccountVo);
	}

    @GetMapping("/list")
	public Result<?> list(ChildrenAccountInput childrenAccountInput) {
        User user = JwtTokenUtil.getCurrentUser();
        childrenAccountInput.setParentId(user.getId());
        childrenAccountInput.setIsAdmin(user.getIsAdmin());
		return childrenAccountService.list(childrenAccountInput);
	}

    @GetMapping("/details/{updateUserId}")
	public Result<?> selectChildrenAccountDetails(@PathVariable Long updateUserId) {
        User user = JwtTokenUtil.getCurrentUser();
		if(user == null){
		    return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
		return childrenAccountService.selectChildrenAccountDetails( updateUserId);
	}

    @GetMapping("/findPermissionByUser")
    public Result<?> findPermissionByUser(){
        User user = JwtTokenUtil.getCurrentUser();
        return permissionService.findPermissionByUser(user.getId());
    }

    @PutMapping("/updatePassword")
    public Result<?> updatePassword(@RequestBody ChildrenAccountVo childrenAccountVo){
        User user = JwtTokenUtil.getCurrentUser();
        if(user == null){
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        return childrenAccountService.updatePassword(childrenAccountVo);
    }

}





